import React, { PureComponent } from 'react';
import moment from 'moment';
import { connect } from 'dva';
import { Table,Avatar,Button,Divider,Form} from 'antd';
import styles from './index.less';
//0ios  1 android
const versionType = ['ios','android'];
@connect(state => ({
  appVersion: state.appVersion,
}))
@Form.create()
class AppVersionTable extends PureComponent {
  state = {
    rsData:[]
  };

  componentWillReceiveProps(nextProps) {
    const { appVersionData: {errorCode,msg,data:{pageNo,pageCount,total,pageSize,list}}, loading } = nextProps;
    this.setState({
      rsData:list
    });
  }
  //table分页、排序、筛选变化时触发
  handleTableChange = (pagination, filters, sorter) => {
    this.props.onChange(pagination, filters, sorter);
  }


  handleGenerateTitle= (total) => {
   return (
     <div>
          <Button type="success" onClick={()=>this.turnToAddVersion()}>创建版本</Button>
          <span style={{marginLeft:20}}>全部版本（{total}）</span>
     </div>
   );
  }

  /**
   * 跳转到 新增页面
   * @param id
   * @param showDetail
   */
  turnToAddVersion(){
    const { dispatch} = this.props;
    dispatch({
      type: 'appVersion/turnToAddVersion'
    });
  }

  updateContent(record) {
    var text = '';
    var content = eval('(' + record.content + ')');
    for(var i = 0;i<content.length;i++) {
      text += (content[i]+'\n')
    }
    return (
      <div style={{marginLeft:12}}>
        {text}
      </div>
    );
  }

  downloadApk(packageName) {
    var apkUrl = "http://139.198.15.159/apk/"+packageName
    return(
      <a href={apkUrl}>下载</a>
    )
  }

  render() {
    const { rsData } = this.state;
    const { appVersionData: {errorCode,msg,data:{pageNo,pageCount,total,pageSize,list}}, loading } = this.props;

    const columns = [
      {
        title: '版本号',
        dataIndex: 'versionCode',
      },
      {
        title: '版本名称',
        dataIndex: 'versionName',
      },{
        title: '更新内容',
        dataIndex: 'content',
        render:(text, record, index) => (
          <div>
            {this.updateContent(record)}
          </div>
        )
      },{
        title: '更新时间',
        dataIndex: 'stime',
      },{
        title: '版本类型',
        dataIndex: 'versionType',
        render:(text, record, index) => (
          <div>
            {versionType[record.versionType]}
          </div>
        )
      },
      {
        title: '操作',
        render: (text,record,index) => (
          <div>
            {this.downloadApk(record.packageName)}
          </div>
        ),
      },
    ];

    const paginationProps = {
      pageSize:pageSize,
      total:total,
      current:pageNo,
    };


    return (
      <div className={styles.msgTable}>
        <Table
          title={() => this.handleGenerateTitle(total)}
          loading={loading}
          rowKey={record => record.id}
          dataSource={rsData}
          columns={columns}
          pagination={paginationProps}
          onChange={this.handleTableChange}
        />
      </div>
    );
  }
}

export default AppVersionTable;
